<?php
namespace app\admin\controller;



class Collage extends Base
{

    public function lists(){
        $list = model('ptActivity')->join('goods g','pt_activity.goods_id = g.id')->field("pt_activity.name as p_name,g.name as g_name,pt_activity.discount,g.price,pt_activity.id as p_id,pt_activity.hours,pt_activity.number")->select();
//        dump(json_decode(json_encode($list)));
        $this->assign('list',$list);
        return view();
    }

    public function edit(){
        $id = i('id');
        $edit = model('ptActivity')->where('id',$id)->find();
//        dump(json_decode(json_encode($edit)));
        $this->assign('edit',$edit);
        return view();
    }
    public function update(){
        $id = i('id');
        $name = i('name');
        $goods = i('goods_id');
        $discount = i('discount');
        $hours = i('hours');
        $number = i('number');
        $res = model('ptActivity')->allowField(true)->where('id',$id)->update(['name'=>$name,'goods_id'=>$goods,'discount'=>$discount,'hours'=>$hours,'number'=>$number]);
        if (!empty($res)){
            $this->success('编辑成功','/admin/collage/lists');
        }
        $this->error('编辑失败');
        return view ();
    }

    public function del(){
        $id = i('id');
        $mod=model('ptActivity')->where("id",$id)->delete();
        if(empty($mod)){
            $this->error("数据不存在");
        }
        $this->success("删除成功","/admin/collage/lists");
    }

    public function add(){//返回添加拼团活动视图
        return view();
    }

    public function add_to(){
        $data = $_POST;
        if (!empty($data)){
            $list = model('ptActivity')->insert($data);
        }
        if ($list){
            $this->success('数据添加成功!','/admin/collage/lists');
        }

    }
}
